#!usr/bin/perl
package UserService;
push (@INC , "D:/workplace/Apache2/mason/Model");
use strict;
use warnings;
use DBI;

my $host = "localhost";         # 主机地址
my $driver = "mysql";           # 接口类型 默认为 localhost
my $database = "test";        # 数据库
# # 驱动程序对象的句柄
my $dsn = "DBI:$driver:database=$database:$host";
my $db_userid = "root";            # 数据库用户名
my $db_password = "root";        # 数据库密码

# 连接数据库
our $dbh = DBI->connect($dsn, $db_userid, $db_password ) or die $DBI::errstr;

sub new{
    my $class = shift;
    my $self = bless {}, $class;

    return $self;
};

sub login{
    my($self, $email, $password) = @_;

    my %data;

    my %result = ('code' => 1000, 'message' => 'Success', 'data' => '');

    my $sth = $dbh->prepare("SELECT * FROM users where email=? limit 1") or die $dbh->errstr;   # 预处理 SQL  语句
    $sth->execute($email) or die $DBI::errstr;    # 执行 SQL 操作

    # 循环输出所有数据
    while ( my $row = $sth->fetchrow_hashref() )
    {
        $data{'id'} = $row->{'id'};
        $data{'email'}  = $row->{'email'};
        $data{'password'}  = $row->{'password'};
        $data{'nickname'}  = $row->{'nickname'};
    }

    if($data{'password'} eq $password)
    {
        $result{'data'} = {'id'=>$data{'id'}, 'nickname'=>$data{'nickname'}};
    }else{
        $result{'code'} = 1001;
        $result{'message'} = 'Wrong account password.';
    }

    return %result;
};

sub register{
    my($self, $params) = @_;
    #params 传递的是一个数组索引
    my $sth = $dbh->prepare("insert into users(email, password, nickname, gender, age) values(?,?,?,?,?)");

    $sth->execute($params->{'email'}, $params->{'password'}, $params->{'nickname'}, $params->{'gender'}, $params->{'age'});

    #获取id (没找到获取新增id的方法)
    $sth = $dbh->prepare("SELECT id FROM users where email=? limit 1") or die $dbh->errstr;   # 预处理 SQL  语句
    $sth->execute($params->{'email'}) or die $DBI::errstr;    # 执行 SQL 操作
    my $id;
    # 循环输出所有数据
    while ( my $row = $sth->fetchrow_hashref() )
    {
        $id = $row->{'id'};
    }

    my %result = (
        'code' => 1000,
        'message' => 'Register Success.',
        'data' => {'id' => $id, 'nickname'=>$params->{'nickname'}}
    );

    return %result;
};

sub checkEmailExists{
    my($self, $email) = @_;

    my %result = (
        'code' => 1000,
        'message' => 'Register Success.',
        'data' => ''
    );

    my $sth = $dbh->prepare("select id from users where email = ?");

    $sth->execute($email);

    while ( my $row = $sth->fetchrow_hashref() )
    {
        if($row->{'id'})
        {
            $result{'code'} = 1005;
            $result{'message'} = 'This Email had been register';

            return %result;
        }
    }

    return %result;
};

sub list{
    my($self) = @_;

    my $sth = $dbh->prepare("select * from users where deleted_at = 0");

    $sth->execute();

    my @data_ =qw//;

    while ( my $row = $sth->fetchrow_hashref() )
    {
        my %data = ();

        $data{'id'}         = $row->{'id'};
        $data{'email'}      = $row->{'email'};
        $data{'password'}   = $row->{'password'};
        $data{'nickname'}   = $row->{'nickname'};
        $data{'gender'}     = $row->{'gender'};
        $data{'age'}        = $row->{'age'};

        push @data_, \%data;
    }

    return @data_;
};

sub detail{
    my($self, $id) = @_;

    my $sth = $dbh->prepare("select * from users where id = ?");

    $sth->execute($id);

    my %data = ();

    while ( my $row = $sth->fetchrow_hashref() )
    {
        $data{'id'} = $row->{'id'};
        $data{'email'}  = $row->{'email'};
        $data{'password'}  = $row->{'password'};
        $data{'nickname'}  = $row->{'nickname'};
        $data{'gender'}  = $row->{'gender'};
        $data{'age'}  = $row->{'age'};
    }

    return %data;
};

sub update{
    my ($self, $params)  = @_;

    if($params->{'password'} eq '')
    {
        my $sth = $dbh->prepare("update users set nickname=?, gender=?,age=? where id=?");

        $sth->execute($params->{'nickname'}, $params->{'gender'}, $params->{'age'}, $params->{'id'});
    }else{
        my $sth = $dbh->prepare("update users set nickname=?,password=?,gender=?,age=? where id=?");

        $sth->execute($params->{'nickname'},$params->{'password'}, $params->{'gender'}, $params->{'age'}, $params->{'id'});
    }


    return 1;
};

sub delete{
    my ($self, $id)  = @_;

    my $sth = $dbh->prepare("update users set deleted_at=? where id=?");

    $sth->execute(time(), $id);

    return 1;
};

# sub DESTROY{
#     my($dbh, $sth) = @_;
#
#     $sth->finish();
#     $dbh->disconnect();
# };

1;